﻿using CleanLite.Domain.Enums.Menu;
using SqlSugar;

namespace CleanLite.Domain.Entities
{
    /// <summary>
    /// 菜单表
    /// </summary>
    [SugarTable("Sys_Menu")]
    public class Sys_Menu : BaseEntity
    {
        /// <summary>
        /// 菜单名称
        /// </summary>
        [SugarColumn(ColumnDescription = "菜单名称")]
        public string MenuName { get; set; } = string.Empty;

        /// <summary>
        /// 图标名称
        /// </summary>
        [SugarColumn(ColumnDescription = "图标名称", IsNullable = true)]
        public string IconName { get; set; }

        /// <summary>
        /// 菜单类型
        /// </summary>
        [SugarColumn(ColumnDescription = "菜单类型")]
        public MenuType Type { get; set; }

        /// <summary>
        /// 路由名称
        /// </summary>
        [SugarColumn(ColumnDescription = "路由名称", IsNullable = true)]
        public string RouteName { get; set; }

        /// <summary>
        /// 组件路径
        /// </summary>
        [SugarColumn(ColumnDescription = "组件路径", IsNullable = true)]
        public string ComponentPath { get; set; }

        /// <summary>
        /// 父级菜单ID
        /// </summary>
        [SugarColumn(ColumnDescription = "父级菜单ID", DefaultValue = "0")]
        public long ParentId { get; set; } = 0;

        /// <summary>
        /// 排序
        /// </summary>
        [SugarColumn(ColumnDescription = "排序", DefaultValue = "0")]
        public int Sort { get; set; } = 0;

        /// <summary>
        /// 是否隐藏菜单
        /// </summary>
        [SugarColumn(ColumnDescription = "是否隐藏菜单", DefaultValue = "0")]
        public bool IsHidden { get; set; } = false;

        /// <summary>
        /// 子菜单集合（非数据库字段）
        /// </summary>
        [SugarColumn(IsIgnore = true)]
        public List<Sys_Menu> Children { get; set; } = [];

        /// <summary>
        /// 是否顶级菜单
        /// </summary>
        /// <returns></returns>
        public bool IsRoot() => ParentId == 0;

        /// <summary>
        /// 添加子菜单
        /// </summary>
        /// <param name="child"></param>
        public void AddChild(Sys_Menu child)
        {
            if (child == null) return;
            child.ParentId = this.Id;
            Children.Add(child);
        }

        /// <summary>
        /// 移除子菜单
        /// </summary>
        public void RemoveChild(long childId)
        {
            var child = Children.FirstOrDefault(c => c.Id == childId);
            if (child != null)
            {
                Children.Remove(child);
            }
        }

        /// <summary>
        /// 获取菜单全路径，例如：系统管理/用户管理/用户详情
        /// </summary>
        public string GetFullPath(Func<long, Sys_Menu> findById)
        {
            var names = new List<string> { MenuName };
            var parentId = ParentId;
            while (parentId != 0)
            {
                var parent = findById(parentId);
                if (parent == null) break;
                names.Insert(0, parent.MenuName);
                parentId = parent.ParentId;
            }
            return string.Join("/", names);
        }

        /// <summary>
        /// 判断是否可显示（非隐藏）
        /// </summary>
        public bool CanShow() => !IsHidden;
    }
}